package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Collage;
import com.yugong.entity.dto.Major;
import com.yugong.entity.dto.OrderClassAdmin;
import com.yugong.entity.vo.request.MajorRequestVO;
import com.yugong.entity.vo.response.MajorListResponseVO;
import com.yugong.mapper.CollageMapper;
import com.yugong.mapper.MajorMapper;
import com.yugong.mapper.OrderClassAdminMapper;
import com.yugong.service.MajorService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 专业业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/11
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private CollageMapper collageMapper;
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 根据订单班管理员工号查询订单班管理员对应的专业
     *
     * @param orderClassAdminNo 订单班管理员工号
     * @return 订单班管理员对应的专业
     */
    @Override
    public List<MajorListResponseVO> findMajorByOrderClassAdminNo(String orderClassAdminNo) {
        OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClassAdminNo);
        if (orderClassAdmin.getOrderClassAdminCollageNo() == null) {
            return null;
        }
        LambdaQueryWrapper<Major> majorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorLambdaQueryWrapper.eq(Major::getMajorCollageNo, orderClassAdmin.getOrderClassAdminCollageNo());
        List<Major> majorList = majorMapper.selectList(majorLambdaQueryWrapper);
        List<MajorListResponseVO> majorListVOListResponse = new ArrayList<>();
        QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
        majorQueryWrapper.eq("major_collage_no", orderClassAdmin.getOrderClassAdminCollageNo());
        Long majorCountNumber = majorMapper.selectCount(majorQueryWrapper);
        for (Major major : majorList) {
            MajorListResponseVO majorListResponseVO = new MajorListResponseVO();
            majorListResponseVO.setMajorNo(major.getMajorNo());
            majorListResponseVO.setMajorName(major.getMajorName());
            majorListResponseVO.setMajorCountNumber(majorCountNumber);
            majorListResponseVO.setMajorCollageNo(major.getMajorCollageNo());
            majorListResponseVO.setMajorCreateMan(major.getMajorCreateMan());
            String createTime = timeUtils.setDateToStrDate(major.getMajorCreateTime(), Const.YYYY_MM_DD);
            majorListResponseVO.setMajorCreateTime(createTime);
            if (major.getMajorUpdateMan() == null) {
                majorListResponseVO.setMajorUpdateMan("未更新");
            } else {
                majorListResponseVO.setMajorUpdateMan(major.getMajorUpdateMan());
            }
            if (major.getMajorUpdateTime() == null) {
                majorListResponseVO.setMajorUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(major.getMajorUpdateTime(), Const.YYYY_MM_DD);
                majorListResponseVO.setMajorUpdateTime(updateTime);
            }
            majorListVOListResponse.add(majorListResponseVO);
        }
        return majorListVOListResponse;
    }

    /**
     * 根据专业名查询专业
     *
     * @param pageNum   当前页
     * @param pageSize  当前页数据总数
     * @param majorName 专业名
     * @return 专业
     */
    @Override
    public PageInfo<MajorListResponseVO> findMajor(Integer pageNum, Integer pageSize, String majorName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Major> majorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorLambdaQueryWrapper
                .like(Major::getMajorName, majorName)
                .orderByDesc(Major::getMajorCreateTime);
        List<Major> majorList = majorMapper.selectList(majorLambdaQueryWrapper);
        QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
        majorQueryWrapper.like("major_name", majorName);
        Long majorCountNumber = majorMapper.selectCount(majorQueryWrapper);
        List<MajorListResponseVO> majorListResponseVOList = new ArrayList<>();
        for (Major major : majorList) {
            MajorListResponseVO majorListResponseVO = new MajorListResponseVO();
            majorListResponseVO.setMajorNo(major.getMajorNo());
            majorListResponseVO.setMajorName(major.getMajorName());
            majorListResponseVO.setMajorCountNumber(majorCountNumber);
            majorListResponseVO.setMajorCollageNo(major.getMajorCollageNo());
            Collage collage = collageMapper.selectById(major.getMajorCollageNo());
            if (collage == null) {
                majorListResponseVO.setMajorCollageName("未填写");
            } else {
                majorListResponseVO.setMajorCollageName(collage.getCollageName());
            }
            majorListResponseVO.setMajorCreateMan(major.getMajorCreateMan());
            String createTime = timeUtils.setDateToStrDate(major.getMajorCreateTime(), Const.YYYY_MM_DD);
            majorListResponseVO.setMajorCreateTime(createTime);
            if (major.getMajorUpdateMan() == null) {
                majorListResponseVO.setMajorUpdateMan("未更新");
            } else {
                majorListResponseVO.setMajorUpdateMan(major.getMajorUpdateMan());
            }
            if (major.getMajorUpdateTime() == null) {
                majorListResponseVO.setMajorUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(major.getMajorUpdateTime(), Const.YYYY_MM_DD);
                majorListResponseVO.setMajorUpdateTime(updateTime);
            }
            majorListResponseVO.setMajorCountNumber(majorCountNumber);
            majorListResponseVOList.add(majorListResponseVO);
        }
        return PageInfo.of(majorListResponseVOList);
    }

    /**
     * 新增专业
     *
     * @param majorRequestVO 专业请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int addMajor(MajorRequestVO majorRequestVO) {
        Major major = new Major();
        major.setMajorNo(majorRequestVO.getMajorNo());
        major.setMajorName(majorRequestVO.getMajorName());
        major.setMajorCollageNo(majorRequestVO.getMajorCollageNo());
        major.setMajorCreateMan(majorRequestVO.getMajorCreateMan());
        major.setMajorCreateTime(new Date());
        return majorMapper.insert(major);
    }

    /**
     * 更新专业
     *
     * @param majorRequestVO 专业请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateMajor(MajorRequestVO majorRequestVO) {
        Major major = new Major();
        major.setMajorNo(majorRequestVO.getMajorNo());
        major.setMajorName(majorRequestVO.getMajorName());
        major.setMajorCollageNo(majorRequestVO.getMajorCollageNo());
        major.setMajorUpdateMan(majorRequestVO.getMajorUpdateMan());
        major.setMajorUpdateTime(new Date());
        return majorMapper.updateById(major);
    }

    /**
     * 删除专业
     *
     * @param majorNo 专业号
     * @return 数据库影响行数
     */
    @Override
    public int deleteMajor(String majorNo) {
        return majorMapper.deleteById(majorNo);
    }
}
